<?php
	require '../../include/library.inc.php';
	require '../../include/admin_login.inc.php';
		
if (!isset($_POST['is_submitted'])) {
	$_POST['is_submitted'] = '';
}
if (!isset($error_message)) {
	$error_message = '';
}	


$redirect = $_SERVER['HTTP_REFERER'];	
	
	if ($_POST['is_submitted']) {
		$error_message = '';
		$redirect = $_POST["redirect"];
    
	$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
	$result_lang = query($sql);
	
	while ($row_lang = mysql_fetch_object($result_lang)) {
	
	
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				
				

	if (!$name)  $error_message .= 'Не е въведено заглавие на '.$row_lang->Name.'<br/>';
	}
	
	if (!$_POST['seo_url'])  $error_message .= 'Не е въведено SEF URL<br/>';
		elseif(!preg_match('/^[0-9A-Za-z\-\_]+$/', $_POST['seo_url'])) $error_message .= 'Въведеният SEF URL е невалиден<br/>';
		
	$sql = "SELECT
				page_id
			FROM
				pages
			WHERE
				sef_url = '".$_POST['seo_url']."'
			  ";
	$result = query($sql);
	if ($row_seo = mysql_fetch_object($result)) $error_message .= 'Въведеният SEF URL вече е използван<br/>';
	
	//ОК
    if(!$error_message) {
		
		
		
		$sql = 'SELECT MAX(order_number)+1 AS order_number FROM pages';
		$row_number = mysql_fetch_assoc(query($sql));
		
		//Създаваме проекта
		$sql = 'INSERT INTO pages
				  (sef_url, published, order_number)
				VALUES
				  (\''.escape($_POST['seo_url']).'\', \''.intval($_POST['published']).'\', \''.$row_number['order_number'].'\')
				  ';
		query($sql);
		$id = mysql_insert_id();
		$sql = 'SELECT lang_id,Name,code
				FROM
				language
				WHERE
				published
					';
		$result_lang = query($sql);
		
		while ($row_lang = mysql_fetch_object($result_lang)) {
			
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
			
			$sql = "INSERT INTO page_description
				  (page_id, lang_id, title, description, meta_key, meta_description)
				VALUES
				  ('".$id."','".$em."', '".escape($name)."', '".escape($description)."', '".escape($meta_k)."', '".escape($meta_d)."')";
				 
		query($sql);
		}
		
		
		
		
		
		$_SESSION['message'] .= 'Страницата е добавена успешно!';
        redirect($redirect);
    }

}
  	
	
	head_admin("Създаване на нова страница");
?>

<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>js/calendarDateInput.js"></script>
<script type="text/javascript" src="<?=HTML_BASE_ADMIN?>tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
	// General options
	language : "bg", 
	mode : "exact",
    elements : "body1,body2,body3,body4,body5",
	theme : "advanced",
	relative_urls : false,
	plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager",

	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen,|,image,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : false,

	// Drop lists for link/image/media/template dialogs
	//template_external_list_url : "js/template_list.js",
	external_link_list_url : "js/link_list.php",
	//media_external_list_url : "js/media_list.js",
	//external_image_list_url : "js/blog_image_list.php"
	
});
</script>
		
            <?php if ($error_message) { ?>
			<div class="message red">
				<h3>Грешка!</h3>
				<?=$error_message?>
			</div>
			<?php } ?>
			
            <div class="topbuttons">
            	<a href="<?=$redirect?>">Обратно</a>
            </div>
			
            <h3>Създаване на нова страница</h3>
			            
            <form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
            <input type="hidden" name="is_submitted" value="1" />
			<input type="hidden" name="redirect" value="<?=$redirect?>" />			
			
<?php 
			
			$sql = 'SELECT 
						lang_id,
						Name,
						code
						
					FROM
						language
					WHERE
						published
					
					';
			$result = query($sql);
			
			$i=0;
			while ($row_lang = mysql_fetch_object($result)) {
				
				
				$em = $row_lang->lang_id;
				eval('$name =  $_POST[\'title_'.$em.'\'];');
				eval('$description =  $_POST[\'description_'.$em.'\'];');
				eval('$meta_k =  $_POST[\'meta_key_'.$em.'\'];');
				eval('$meta_d =  $_POST[\'meta_description_'.$em.'\'];');
				
								  
			?>
            <dl class="clearfix">
            <h3>Данни на  <?=$row_lang->Name?></h3>
            <dt><label class="required">Заглавие на <?=$row_lang->Name?></label></dt>
            <dd><input class="inputbox w500" name="title_<?=$row_lang->lang_id?>" type="text" value="<?=$name?>" ></dd>
            
            <dt><label>Описание на <?=$row_lang->Name?>(максимална широчина 960px)</label></dt>
            <dd><textarea id="body<?=$row_lang->lang_id?>"  class="inputbox" style="width:100%" name="description_<?=$row_lang->lang_id?>" rows="16"><?=sth($description)?></textarea></dd>
            
            
			<dt><label >Мета описание на <?=$row_lang->Name?>(до 140 символа)</label></dt>
            <dd><textarea class="inputbox w500" name="meta_description_<?=$row_lang->lang_id?>" rows="3" maxlength="140"><?=$meta_d?></textarea></dd>
			
            <dt><label >Ключови думи на <?=$row_lang->Name?>(до 140 символа)</label></dt>
            <dd><textarea class="inputbox w500" name="meta_key_<?=$row_lang->lang_id?>" rows="3" maxlength="140"><?=$meta_k?></textarea></dd>
			
            			
			</dl>
                        
            
			 <?php
				$i++;
			}
			
	  		?>
            		
			<dt><label class="required">SEO URL (на латиница, без интервали Пример: this-is-test)</label></dt>
            <dd><input class="inputbox w500" name="seo_url" type="text" value="<?=$_POST['seo_url']?>" ></dd>
			<input class="checkbox" name="published" id="published" type="checkbox" value="1" <?=(($_POST['published'])?"checked=\"checked\"":"")?> />
			Публикуван</label></dd>
            
            <dd class="submit">
            <input type="submit" name="submit" id="submit" value="създай">
            </dd>
			
			</dl>			
			</form>  
            
<?php
	foot_admin(); 
?>